package com.vrv.vap.baseline.controller;

import com.vrv.vap.baseline.service.impl.ActReDeploymentExportService;
import com.vrv.vap.baseline.util.FileOperationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/act_re_deployment")
public class ActReDeploymentController {
    private static final Logger log = LoggerFactory.getLogger(ActReDeploymentController.class);
    private final ActReDeploymentExportService actReDeploymentExportService;
    private final FileOperationUtil fileOperationUtil;

    public ActReDeploymentController(ActReDeploymentExportService actReDeploymentExportService,
                                    FileOperationUtil fileOperationUtil) {
        this.actReDeploymentExportService = actReDeploymentExportService;
        this.fileOperationUtil = fileOperationUtil;
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        try {
            String zipFile = actReDeploymentExportService.exportZip();
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=act_re_deployment.zip");
            fileOperationUtil.writeFileToResponse(zipFile, response);
        } catch (Exception e) {
            log.error("导出数据失败", e);
            throw new RuntimeException("导出数据失败", e);
        }
    }
}